1 <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Default.aspx.vb" Inherits="VBASPNETExcelLikeGridView._Default" %>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head id="Head1" runat="server">
7 <title>VBExcelLikeGridView</title>
8 <script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.4.4.min.js" type="text/javascript">
10 <script type="text/javascript">
12 //Function to read HidState (JSON) to keep the state color
13 function ResetColors(color1, color2) {
14 var contents = $(":hidden:last").val();
16 // If not "[]", meaning something changed, reset colors.
17 if (contents.toString() != "[]") {
18 // Convert to JSON object.
19 var objectc = eval(contents);
21 // To reset colors depending on whether it's changed
22 for (var i = 0; i < objectc.length; ++i) {
23 if (objectc[i].Color != '') {
24 $("td:eq(" + objectc[i].Index + ")").css("background-color", objectc[i].Color);
27 var checked = objectc[i].Deleted == "True";
29 if (parseInt(objectc[i].Index) % 2 == 0) {
30 $("td:eq(" + parseInt(objectc[i].Index - 2) + ")").find(":checkbox").attr("checked", checked);
33 $("td:eq(" + parseInt(objectc[i].Index - 3) + ")").find(":checkbox").attr("checked", checked);
36 // If a checkbox checked, change the row color
38 $("td:eq(" + objectc[i].Index + ")").parent().css("background-color", "red");
46 function SaveValidate() {
47 //first tell whether you've missed "Name" to be ful-filled...
49 if (Page_ClientValidate('Fill')) {
50 return confirm('Do you really want to save all these changes together?');
53 alert("Attention! You cannot leave a name blank!");
57 //Validation for Insert
58 function InsertValidate() {
59 //first tell whether you've missed "Name" to be ful-filled...
61 if (!Page_ClientValidate('Insert')) {
62 alert("Attention! You cannot insert a blank name!");
66 // Add dynamically events for all textboxes
67 // except the footer one to turn the background color.
68 function AddEvents() {
70 var rowarray = $("tr");
71 for (var i = 0; i < rowarray.length - 1; ++i) {
72 $(rowarray[i]).find(":text").change(function () {
73 $(this).parent().css("background", "blue");
81 //Keep the original color row for odd
82 var color1 = $("tr:eq(1)").css("background-color").valueOf();
83 var color2 = $("tr:eq(2)").css("background-color").valueOf();
84 var headercolor = $("tr:first").css("background-color").valueOf();
85 var footercolor = $("tr:last").css("background-color").valueOf();
89 // Header checkbox's cascading effect:
90 $("#chkAll").click(function () {
92 $(":checkbox").attr("checked", $(this).attr("checked"));
94 if ($(this).attr("checked")) {
95 $(":checkbox").parent().parent().css("background-color", "red");
96 //Reset the color of header
97 $("tr:first").css("background-color", headercolor);
100 $("tr:odd").css("background-color", color1);
101 $("tr:even").css("background-color", color2);
103 //Reset the color of header and footer
104 $("tr:first").css("background-color", headercolor);
105 $("tr:last").css("background-color", footercolor);
109 //Single checkbox checked event
110 $(":checkbox").click(function () {
111 if ($(this).attr("checked")) {
112 $(this).parent().parent().css("background-color", "red");
115 if ($(this).parent().parent().index() % 2 == 0) {
116 $(this).parent().parent().css("background-color", color2);
119 $(this).parent().parent().css("background-color", color1);
123 //Reset the header color
124 $("tr:first").css("background-color", headercolor);
127 ResetColors(color1, color2);
133 <form id="form1" runat="server">
135 Demo for Batching Actions</h1>
136 <span style="color: Red">red row to be deleted</span>
138 <span style="color: green">green row to be added</span>
140 <span style="color: blue">blue cell to be modified</span>
144 <asp:GridView ID="GridView1" runat="server" Width="70%" Height="50%" AutoGenerateColumns="False"
145 CellPadding="4" ForeColor="#333333" GridLines="None" ShowFooter="True">
146 <AlternatingRowStyle BackColor="White" />
148 <asp:TemplateField HeaderText="Delete State">
150 <input id="chkAll" type="checkbox" />
154 <asp:CheckBox ID="chkDelete" runat="server" />
157 <asp:TemplateField HeaderText="Id">
162 Name:<asp:TextBox ID="tbNewName" runat="server"></asp:TextBox>
163 <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbNewName"
164 ErrorMessage="You cannot insert a balnk name!" ForeColor="#FFFF66" ValidationGroup="Insert"></asp:RequiredFieldValidator>
167 <asp:TemplateField HeaderText="Name">
169 <asp:TextBox ID="tbName" runat="server" Text='<%#Eval("PersonName") %>'>
171 <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="tbName"
172 ErrorMessage="You cannot leave a name blank!" ValidationGroup="Fill"></asp:RequiredFieldValidator>
175 Address:<asp:TextBox ID="tbNewAddress" runat="server"></asp:TextBox>
178 <asp:TemplateField HeaderText="Address">
180 <asp:TextBox ID="tbAddress" runat="server" Text='<%#Eval("PersonAddress") %>'>
184 <asp:Button ID="btnAdd" runat="server" Text="Add a new row" OnClick="btnAdd_Click"
185 ValidationGroup="Insert" OnClientClick="InsertValidate()" />
189 <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"
190 VerticalAlign="Middle" />
191 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" HorizontalAlign="Center"
192 VerticalAlign="Middle" />
193 <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
194 <RowStyle BackColor="#FFFBD6" ForeColor="#333333" HorizontalAlign="Center" VerticalAlign="Middle" />
195 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
198 <asp:Button ID="btnSaveAll" runat="server" Height="30px" Text="Save All Changes"
199 Width="149px" OnClick="btnSaveAll_Click" OnClientClick="SaveValidate()" ValidationGroup="Fill" />
200 <asp:HiddenField ID="HidState" runat="server" Value="[]" />